library(ggplot2)

## figure 1
dat = read.csv("figure1data.csv")

ggplot(dat,
       aes(x=factor(variable), 100*value, fill=variable)) + 
  facet_wrap(~State, scales = "free_x") + geom_col() + xlab("") + 
  ylab("Percent of Counties")   + guides(fill=FALSE) +
  scale_fill_manual(values=c("Light Blue", "Dark Blue")) + 
  ggtitle("Most recent election (2015-2018)") +
  theme_bw(base_size = 22) 
ggsave("figure1_part_mismatch.tiff",
       dpi=300, width = 12, height = 8, units="in")

ggplot(dat,
       aes(x=factor(variable), 100*value, fill=variable)) + 
  facet_wrap(~State, scales = "free_x") + geom_col() + xlab("") + 
  ylab("Percent of Counties")   + guides(fill=FALSE) +
  scale_fill_manual(values=c("Light Gray", "Dark Gray")) + 
  ggtitle("Most recent election (2015-2018)") +
  theme_bw(base_size = 22) 
ggsave("figure1_part_mismatch_gray.tiff",
       dpi=300, width = 12, height = 8, units="in")


## figure 2
dat = read.csv('figure2data.csv')
dat$index = 1:26
dat$Inc = "Challenger"
dat[dat$Incumbent==1, "Inc"] = "Incumbent"
dat$Nonpartisan = "No"
dat[dat$NP==1, "Nonpartisan"] = "Yes"
colnames(dat)[1] = "County"
dat = dat[dat$County!="Orange County, CA",]
dat$Winner = ifelse(dat$Outcome>50, 1, 0)

ggplot(dat, aes(Inc, Outcome,  fill=Party)) + geom_col() +
  geom_text(aes(label=Candidate, color=Nonpartisan), vjust=-1) +
  facet_wrap(~Election+County) + 
  ylab("Vote Share \n") + xlab("") +
  scale_color_manual(values=c("Purple", "Dark Green")) +
  scale_fill_manual(values=c("Blue", "Gray", "Red")) + theme_bw()
ggsave("figure2_immigrraces.tiff", width=12, height=8, dpi=300)

ggplot(dat, aes(Inc, Outcome,  fill=Party)) + geom_col() +
  geom_text(aes(label=Candidate, color=Nonpartisan), vjust=-1) +
  facet_wrap(~Election+County) + 
  ylab("Vote Share \n") + xlab("") +
  scale_color_manual(values=c("Dark grey", "Light grey")) +
  scale_fill_manual(values=c("Blue", "Gray", "Red")) + theme_bw() +
  scale_fill_grey()
ggsave("figure2_immigrraces_gray.tiff", width=12, height=8, dpi=300)

